package com.cn.kabalifeuser.ui.integral.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.config.CustomRefreshListener;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.config.RefreshHelper;
import com.cn.kabalifeuser.ui.integral.adapter.ShoppingMallAdapter;
import com.cn.kabalifeuser.ui.integral.bean.BannerActBean;
import com.cn.kabalifeuser.ui.integral.bean.ShoppingMalListBean;
import com.cn.kabalifeuser.ui.integral.fragment.ShoppingMallPresenter;
import com.cn.kabalifeuser.ui.integral.fragment.ShoppingMallView;
import com.cn.kabalifeuser.ui.service.bean.BannerListBean;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.view.NotDataView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 晓林 on 2020/10/10.
 * function: 商品的搜索
 */
public class CommoditySearchActivity extends IBaseActivity<ShoppingMallView, ShoppingMallPresenter> implements ShoppingMallView {

    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.rv_notice)
    RecyclerView recyclerView;

    List<ShoppingMalListBean.ResultBean.DataBean> mList;
    ShoppingMallAdapter mAdapter;
    private View notDataView;


    Bundle bundle;
    int category_id = 0;
    int sign;

    @Override
    protected ShoppingMallPresenter createPresenter() {
        return new ShoppingMallPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_commodity_search_list;
    }

    @Override
    public void leftFinish() {
        super.leftFinish();
        hideKeyboard(etSearch);
        finish();
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBar("搜索");
        bundle = getIntent().getExtras();
        sign = bundle.getInt("sign");
        if (sign > 1)
            category_id = bundle.getInt("category_id");
        mList = new ArrayList<>();
        recyclerView.setLayoutManager(new GridLayoutManager(getMe(), 2));
        recyclerView.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new ShoppingMallAdapter(R.layout.fragment_mall_item, mList, 1);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(mAdapter);
        notDataView = NotDataView.notDataView(this, recyclerView, refreshLayout);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //todo 跳转详情
                Bundle bundle = new Bundle();
                bundle.putInt("id", mAdapter.getItem(position).getId());
                $startActivity(IntegralCommodityDetailsActivity.class, bundle);
            }
        });
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String key = etSearch.getText().toString().trim();
                    if (!TextUtils.isEmpty(key)) {
                        etSearch.clearFocus();
                        if (!TextUtils.isEmpty(key)) {
                            hideKeyboard(etSearch);
                            refreshLayout.autoRefresh();
                        } else {
                            refreshLayout.autoRefresh();
                        }
                    }
                }
                return false;
            }
        });
        pagehttp = 1;
        getListData();
    }

    @Override
    public void initRefreshLayout() {
        super.initRefreshLayout();
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    private void getListData() {
        mPresenter.onShoppingMallData(LoginHelper.getLonginData().getResult(), etSearch.getText().toString().trim(), category_id, pagehttp, pageNum);
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return getMe();
    }

    @Override
    public void onBannerActSuccess(BannerActBean bean) {

    }

    @Override
    public void onBannerSuccess(BannerListBean bean) {

    }

    @Override
    public void onShoppingMalListSuccess(ShoppingMalListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.replaceData(bean.getResult().getData());
            } else {
                mAdapter.replaceData(bean.getResult().getData());
                mAdapter.setEmptyView(notDataView);
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.addData(bean.getResult().getData());
            }

        }
    }
}
